import router from './router'
import store from './store'
import settings from './settings';
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
// import getPageTitle from '@/utils/get-page-title'

NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/login'] // no redirect whitelist

const platform = process.env.VUE_APP_BASE_NAME

router.beforeEach(async (to, from, next) => {
  NProgress.start()

  document.title = settings.title

  const hasToken = store.state.app.token

  if (hasToken) {
    if (to.path === '/login') {
      next({ path: '/notice' })
      NProgress.done()
    } else {
      const menuList = store.getters.menuList
      const hasMenuList = menuList.length > 0
      if (hasMenuList) {
        next()
      } else {
        // try {
        //   next({ ...to, replace: true })
        // } catch (error) {
        //   next(`/login`)
        //   NProgress.done()
        // }
      }
    }
  } else {
    /* has no token*/
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next(`/login`)
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  NProgress.done()
})
